package training;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author liangzai
 * @Description:
 */
public class J25_5_25_Training {
    //连接两字母单词得到的最长回文串
    public int longestPalindrome(String[] words) {
        Map<String,Integer> set = new HashMap<>();
        int sum = 0;
        int nums = 0;
        for (int i = 0; i < words.length; ++i) {
            if (words[i].charAt(0) == words[i].charAt(1)) {
                nums++;
            }
            String s = "" + words[i].charAt(1) + words[i].charAt(0);
            if (set.containsKey(s) && set.get(s)>0) {
                if (words[i].charAt(0) == words[i].charAt(1)) {
                    nums -= 2;
                }
                sum += 4;
                set.put(s,set.get(s)-1);
            } else {
                set.put(words[i],set.getOrDefault(words[i],0)+1);
            }

        }
        return sum + (nums > 0 ? 2 : 0);
    }
}
